package com.example.sports_meet.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.example.sports_meet.InformationActivity;
import com.example.sports_meet.MyDbHelper;
import com.example.sports_meet.R;
import com.example.sports_meet.athlete.AthleteQuery;
import com.example.sports_meet.athlete.AthleteSignUp;
import com.example.sports_meet.volunteer.VolunteerSignUp;

import java.util.ArrayList;
import java.util.List;

public class FirstFragment extends Fragment {
    private Button athlete1, athlete2, volunteer, information;
    private TextView tv_result;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_first, container, false);
        athlete1 = view.findViewById(R.id.btn_athlete1);
        athlete2 = view.findViewById(R.id.btn_athlete2);
        volunteer = view.findViewById(R.id.btn_volunteer);
        information = view.findViewById(R.id.btn_information);
        tv_result = view.findViewById(R.id.tv_result);
        // 添加新数据并将其显示在 TextView 中
        Object[] newSignupData = {"202201002", "202204001", "2022-06-13 15:30:00", "是", "是"};
        addNewSignupData(newSignupData);


        athlete1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到运动员报名界面
                Intent intent = new Intent(getActivity(), AthleteSignUp.class);
                startActivity(intent);
            }
        });
        athlete2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到运动员查询界面
                Intent intent = new Intent(getActivity(), AthleteQuery.class);
                startActivity(intent);
            }
        });
        volunteer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到志愿者报名界面
                Intent intent = new Intent(getActivity(), VolunteerSignUp.class);
                startActivity(intent);
            }
        });
        information.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到信息修改界面
                Intent intent = new Intent(getActivity(), InformationActivity.class);
                startActivity(intent);
            }
        });


        return view;
    }

    //Lint 检查器可能会警告 getColumnIndex() 方法可能返回 -1，但实际上代码中没有发生此情况。选择忽略此 Lint 警告 @SuppressLint("Range")
    @SuppressLint("Range")
    //查询并显示运动员的报名信息：
    private String queryAllSignupResults() {
        MyDbHelper dbHelper = new MyDbHelper(getContext(), "db", null, 2);
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        //查询signup表，并连接athlete和eevent表，获取ano,aname和对应的ename;
        //INNER JOIN 是 SQL 语言中的一种连接（JOIN）操作，用于根据两个或多个表中的相等条件合并行。
        String query = "SELECT athlete.ano, athlete.aname, eevent.ename FROM signup " +
                "INNER JOIN athlete ON signup.ano = athlete.ano " +
                "INNER JOIN eevent ON signup.eno = eevent.eno";
        Cursor cursor = db.rawQuery(query, null);

        //将查询结果添加到列表中
        List<String> signupResultList = new ArrayList<>();
        String[] columnNames = cursor.getColumnNames();
        for (String columnName : columnNames) {
            System.out.println("列名" + columnName);
        }
        while (cursor.moveToNext()) {
            String[] columnNames1 = cursor.getColumnNames();
            for (String columnName : columnNames1) {
                int columnIndex = cursor.getColumnIndex(columnName);
                String value = cursor.getString(columnIndex);
                System.out.println(columnName + ": " + value);
            }
            String ano = cursor.getString(cursor.getColumnIndex("ano"));
            String aname = cursor.getString(cursor.getColumnIndex("aname"));
            String ename = cursor.getString(cursor.getColumnIndex("ename"));

            signupResultList.add(aname + " (" + ano + ") 报名参加了 " + ename);
            System.out.println(aname + " (" + ano + ") 报名参加了 " + ename);

        }


        cursor.close();
        dbHelper.close();

        //将报名结果转化为一个字符串

        StringBuilder stringBuilder = new StringBuilder();
        for (String result : signupResultList) {
            stringBuilder.append(result);
            stringBuilder.append("\n");
        }

        return stringBuilder.toString();

    }

    public void updateTextViewData() {
        String signupResults = queryAllSignupResults();
        tv_result.setText(signupResults);
    }

    public void addNewSignupData(Object[] newData) {
        // 获取数据库实例
        MyDbHelper dbHelper = new MyDbHelper(getContext(), "db", null, 2);
        SQLiteDatabase db = dbHelper.getWritableDatabase();

        // 在这里执行添加新数据的逻辑
        String sql = "INSERT OR IGNORE INTO signup (ano, eno, sutime, suwhether, suvalid) VALUES (?, ?, ?, ?, ?)";
        db.execSQL(sql, newData);

        // 更新 TextView
        updateTextViewData();
    }
}